<?php 

require_once './php/core/p_pre.php';
process($db, $userId, $deviceId);
require_once './php/core/p_post.php';

function process(&$db, $userId, $deviceId) {
	require_once './php/core/message.php';	
	
	$isPostBack = param('isPostBack');
	$datasetId = param('datasetId');
	$lang = param('lang');
	if ( $datasetId < 1 || strlen($lang) < 1 ) {
		return;
	}	
	$message = new UMessage();
	$prevMsg = $message->findByDatasetId($db, $datasetId, $lang);	
	if ( strlen($isPostBack)) {
		saveMessage($db, $prevMsg, $datasetId, $lang);
		header("Location: edit_lang.php?datasetId=".$datasetId."&lang=".$lang);
		return;
	}
	
	$language;
	$lang = param('lang');
	$defaultValues = array();
	switch ($lang) {
		case 'cn':
			$language = 'Chinese';
	//		$defaultValues[0] = createMessage('characters', '特征');
	//		$defaultValues[1] = createMessage('selected', '已使用的特征');
	//		$defaultValues[2] = createMessage('taxa', '分类单位');
	//		$defaultValues[3] = createMessage('matched', '已找到的分类单位');
			break;
		case 'de':
			$language = 'German';
			break;		
	}
	
	$title = '';
	$author = '';
	if ( $prevMsg != null ) {
		$title = CROUtil::nullToNone($prevMsg->title);
		$author = CROUtil::nullToNone($prevMsg->author);
	}

?>

<!DOCTYPE html> <!-- Destop version -->
<html manifest="cache.manifest">
<head>

<?php require_once 'tile_head.php'; ?>

<link rel="stylesheet" href="css/uKey_d.css" />
<link rel="stylesheet" href="css/slides_show_d.css" />
<script type="text/javascript" src="js/uEditMessage.js"></script>

<script type="text/javascript">
	$( '#uPageEditLang' ).live( 'pageinit',function(event){
		UEditMessage.getInstance().init(false);
		UEditMessage.getInstance().loadData();
	});
	
	
</script>

</head>
<body>
<form name="uFeedbackForm" data-ajax="false" action="" method="post">
	<div data-role="page" data-title="Loading..." id="uPageEditLang">
		<div data-theme="b" data-role="header" style="text-transform: capitalize;">
			<a href="index_d.php" data-ajax="false" data-icon="home">Home</a>
			<h1 id="uTitle">Loading...</h1>	
		</div>

		<div id="uContent" data-role="content">
			<div  style="text-align: center;"  id="uInkURL" style="font-weight: bold;font-size: 100%"></div>
			<h2>Translate into <?php echo $language; ?></h2>
			<div id="uSubTitle" style="text-transform: capitalize;"></div>
		
			<input type="hidden" name="isPostBack" value="1"/>
			<input type="hidden" name="datasetId" value="<?php echo param('datasetId'); ?>"/>
			<input type="hidden" name="lang" value="<?php echo param('lang'); ?>"/>
			<input type="submit" data-inline="true" data-theme="e" value="Save"/> 
			<br/>
			<input type="hidden" name="key[]" value="title"/>
			<div>	
			Dataset Title <input type="text" size="40" data-role="none" name="v_title" value="<?php echo $title; ?>"/>
			
			&nbsp; &nbsp; &nbsp; &nbsp; Your Name:<input type="hidden" name="key[]" value="author"/>	
	<input type="text" data-role="none" size="40"  name="v_author" value="<?php echo $author; ?>"/>	
			
			</div>	
			<div id="uPK_content"></div>
			
			<input type="submit" data-inline="true" data-theme="e" value="Save"/>  
<?php 
	for ($i=0; $i<count($defaultValues); $i++) {
		$d = $defaultValues[$i];
?>
	<input type="hidden" name="key[]" value="<?php echo $d->k; ?>"/>	
	<input type="hidden" data-role="none" name="<?php echo 'v_'.$d->k; ?>" value="<?php echo $d->v; ?>"/>	
			
<?php } ?>
		</div>
	</div>
</form>	
</body>
</html>

<?php 

}
	
function saveMessage(&$db, $prevMsg, $datasetId, $lang) {	
	$keys = param('key');
	$total = count($keys);
	if ( $total < 1 ) {
		return;
	}
	
	$list = array();
	$cnt = 0;
	for ($i=0; $i<$total; $i++) {
		$key = $keys[$i];
		$value = param('v_'.$key);
		if ( strlen($value) < 1) {
			continue;
		}
		//echo $key.'  ===  '.$value.'<br/>';
		$m = new UMessageTemp();
		$m->k = $key;
		$m->v = $value;
		$list[$cnt] = $m;
		$cnt++;
	}
	
	$m = new UMessageTemp();
	$m->k = 'lang';
	$m->v = $lang;
	$list[count($list)] = $m;
	
	$jsonStr = json_encode($list);
	$title = param('v_title');
	$author = param('v_author');
	
	$message = new UMessage();
	if ( $prevMsg ) {
		$message->updateChinese($db, $datasetId, $lang, $jsonStr, $title, $author);
	} else {
		$message->insertChinese($db, $datasetId, $lang, $jsonStr, $title, $author);
	}
}


class UMessageTemp {
	var $k;
	var $v;
}

function createMessage($k, $v) {
	$m = new UMessageTemp();
	$m->k = $k;
	$m->v = $v;
	return $m;
}

?>